#-------------------------------------------------------------------
# This file is part of the CMake build system for MP Galaxy
#     (Multi Player Galaxy Game)
# For the latest info, see http://www.MPG3d.org/
#
# The contents of this file are placed in the public domain. Feel
# free to make use of it in any way you like.
#-------------------------------------------------------------------

####################################
#  MP-Galaxy BUILD SYSTEM 
#  Project: MP Galaxy MAIN project
####################################

cmake_minimum_required(VERSION 2.6)
set(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)
cmake_policy(SET CMP0003 NEW)

project(MPG)

# Include necessary submodules
set(CMAKE_MODULE_PATH 
  "${MPG_SOURCE_DIR}/CMake"
  "${MPG_SOURCE_DIR}/CMake/Utils" 
  "${MPG_SOURCE_DIR}/CMake/Packages"
)

include(CMakeDependentOption)
include(CheckCXXCompilerFlag)
include(MacroLogFeature)
include(MpgConfigTargets)
include(PreprocessorUtils)
set(MPG_TEMPLATES_DIR "${MPG_SOURCE_DIR}/CMake/Templates")
set(MPG_WORK_DIR ${MPG_BINARY_DIR})


#####################################################################
# Set up the basic build environment
#####################################################################

if (CMAKE_BUILD_TYPE STREQUAL "")
  # CMake defaults to leaving CMAKE_BUILD_TYPE empty. This screws up
  # differentiation between debug and release builds.
  set(CMAKE_BUILD_TYPE "RelWithDebInfo" CACHE STRING "Choose the type of build, options are: None (CMAKE_CXX_FLAGS or CMAKE_C_FLAGS used) Debug Release RelWithDebInfo MinSizeRel." FORCE)
endif ()

# determine MPG version numbers
include(MpgGetVersion)
mpg_get_version("${MPG_SOURCE_DIR}/Core/Interface/Version.h")
message(STATUS "Configuring MP-Galaxy ${MPG_VERSION}")

if (NOT APPLE)
  # Create debug libraries with _d postfix
  set(CMAKE_DEBUG_POSTFIX "_d")
endif ()

# Set compiler specific build flags
if (CMAKE_COMPILER_IS_GNUCXX)
  check_cxx_compiler_flag(-msse MPG_GCC_HAS_SSE)
  if (MPG_GCC_HAS_SSE)
    add_definitions(-msse)
  endif ()
endif ()
if (MSVC)
  add_definitions(/fp:fast)
endif ()

if (CMAKE_COMPILER_IS_GNUCXX)
  # Test for GCC visibility
  include(CheckCXXCompilerFlag)
  check_cxx_compiler_flag(-fvisibility=hidden MPG_GCC_VISIBILITY)
  if (MPG_GCC_VISIBILITY)
    # determine gcc version
    execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion 
      OUTPUT_VARIABLE MPG_GCC_VERSION)
    message(STATUS "Detected g++ ${MPG_GCC_VERSION}")
    message(STATUS "Enabling GCC visibility flags")
    set(MPG_GCC_VISIBILITY_FLAGS "-DMPG_GCC_VISIBILITY -fvisibility=hidden")
  
    # check if we can safely add -fvisibility-inlines-hidden
    string(TOLOWER "${CMAKE_BUILD_TYPE}" MPG_BUILD_TYPE)
    if (MPG_BUILD_TYPE STREQUAL "debug" AND MPG_GCC_VERSION VERSION_LESS "4.2")
      message(STATUS "Skipping -fvisibility-inlines-hidden due to possible bug in g++ < 4.2")
    else ()
      set(MPG_GCC_VISIBILITY_FLAGS "${MPG_GCC_VISIBILITY_FLAGS} -fvisibility-inlines-hidden")
    endif ()
  endif (MPG_GCC_VISIBILITY)
  
  # Fix x64 issues on Linux
  if("${CMAKE_SYSTEM_PROCESSOR}" STREQUAL "x86_64" AND NOT APPLE)
    add_definitions(-fPIC)
  endif()  
endif (CMAKE_COMPILER_IS_GNUCXX)

# determine system endianess
include(TestBigEndian)
test_big_endian(OGRE_TEST_BIG_ENDIAN)

# Find dependencies
include(Dependencies)

# Setup sub projects
#--------------------
add_subdirectory(Util)
add_subdirectory(Core)
add_subdirectory(Modules)
add_subdirectory(Client)
